<?php
use think\facade\Db;
require_once 'lizhilitag.php';
//计算距离现在多长时间
function tonow($data)
{
    $data=time()-$data;
    //计算天数
    $days = intval($data/86400);
    //计算小时数
    $remain = $data%86400;
    $hours = intval($remain/3600);
    //计算分钟数
    $remain = $data%3600;
    $mins = intval($remain/60);
    //计算秒数
    $secs = $data%60;
    return $days.'天'.$hours.'小时'.$mins.'分钟'.$secs.'秒';
}
function jiequ($data, $num=50)
{
    if(mb_strlen($data) > $num){
        return mb_substr($data, 0, $num).'...';
    }else{
        return mb_substr($data, 0, $num);
    }
}
function obs($data)
{
    return $data;
}
function kong($str)
{
    $str=trim($str);
    $str = mb_ereg_replace('^(　| )+', '', $str);
    $str = mb_ereg_replace('(　| )+$', '', $str);
    $str = mb_ereg_replace('　　', "\n　　", $str);
    return $str;
}
function img($data)
{
    if ($data) {
        return $data;
    } else {
        return '/static/moren.jpg';
    }
}
//加密
function jiami($data, $key='lizhili'){
    $key    =    md5($key);
    $x        =    0;
    $len    =    strlen($data);
    $l        =    strlen($key);
    $char=$str='';
    for ($i = 0; $i < $len; $i++)
    {
        if ($x == $l)
        {
            $x = 0;
        }
        $char .= $key[$x];
        $x++;
    }
    for ($i = 0; $i < $len; $i++)
    {
        $str .= chr(ord($data[$i]) + (ord($char[$i])) % 256);
    }
    return base64_encode($str);
}
//解密
function jiemi($data, $key='lizhili')
{
    $key = md5($key);
    $x = 0;
    $data = base64_decode($data);
    $len = strlen($data);
    $l = strlen($key);
    $char=$str='';
    for ($i = 0; $i < $len; $i++)
    {
        if ($x == $l)
        {
            $x = 0;
        }
        $char .= substr($key, $x, 1);
        $x++;
    }
    for ($i = 0; $i < $len; $i++)
    {
        if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1)))
        {
            $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
        }
        else
        {
            $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
        }
    }
    return $str;
}